<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="120px"
    >
      <el-form-item label="老人" prop="oldManName">
        <el-input
          v-model="queryParams.oldManName"
          placeholder="请输入老人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="出生日期" prop="dateBirth">
        <el-date-picker
          clearable
          v-model="queryParams.dateBirth"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="出生日期"
          @keyup.enter.native="handleQuery"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="户籍地址" prop="city">
        <el-cascader
          v-model="queryParams.city"
          :options="area"
          :props="cityProps"
          placeholder="请选择户籍地址"
          clearable
        ></el-cascader>
      </el-form-item>
      <el-form-item label="民族" prop="nation">
        <el-select
          v-model="queryParams.nation"
          placeholder="请选择民族"
          clearable
        >
          <el-option
            v-for="dict in dict.type.user_nation"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
        <el-form-item label="婚姻状况" prop="maritalStatus">
          <el-select
            v-model="queryParams.maritalStatus"
            placeholder="请选择婚姻状况"
            clearable
          >
            <el-option
              v-for="dict in dict.type.user_marriage_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="文化程度" prop="standardCulture">
          <el-select
            v-model="queryParams.standardCulture"
            placeholder="请选择文化程度"
            clearable
          >
            <el-option
              v-for="dict in dict.type.user_culture_extent"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:condition:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:condition:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:condition:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:condition:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="conditionList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="oldManName" />
      <el-table-column label="联系方式" align="center" prop="phone" />
      <el-table-column label="证件类型" align="center" prop="certificateType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.user_documents_type"
            :value="scope.row.certificateType"
          />
        </template>
      </el-table-column>
      <el-table-column label="证件号码" align="center" prop="idNumber" />
      <el-table-column label="出生年月" align="center" prop="dateBirth" />
      <el-table-column label="户籍地址" align="center" prop="city">
        <template slot-scope="scope">
          {{ scope.row.residenceProvinceName }}{{ scope.row.residenceCityName
          }}{{ scope.row.residenceAreaName }}
        </template>
      </el-table-column>
      <el-table-column label="民族" align="center" prop="nation">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.user_nation" :value="scope.row.nation" />
        </template>
      </el-table-column>
      <el-table-column label="婚姻状况" align="center" prop="maritalStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.user_marriage_type"
            :value="scope.row.maritalStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="文化程度"
        align="standardCulture"
        prop="historyDrugAllergyHistory"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.user_culture_extent"
            :value="scope.row.standardCulture"
          />
        </template>
      </el-table-column>
      <el-table-column label="职业" align="center" prop="occupation">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.user_occupation" :value="scope.row.occupation" />
        </template>
      </el-table-column>

      <el-table-column label="退休" align="center" prop="isRetire">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.user_retire" :value="scope.row.isRetire" />
        </template>
      </el-table-column>
      <el-table-column label="健在" align="center" prop="isSurvival">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.user_ok" :value="scope.row.isSurvival" />
        </template>
      </el-table-column>
      <el-table-column label="政府救助类型" align="center" prop="governmentRescue">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.user_bailout_type"
            :value="scope.row.governmentRescue"
          />
        </template>
      </el-table-column>
      <el-table-column label="主要经济来源" align="center" prop="sourceFinance">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.user_economy" :value="scope.row.sourceFinance" />
        </template>
      </el-table-column>
      <el-table-column label="头像" align="center" prop="avatar">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="server + scope.row.avatar"
            :preview-src-list="[server + scope.row.avatar]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="家属信息" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleStatusDetail(scope.row)"
            v-hasPermi="['system:condition:edit']"
            >健康信息</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="家属信息" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleAddDetail(scope.row)"
            v-hasPermi="['system:condition:edit']"
            >查看家属</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="居住信息" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDetail(scope.row)"
            v-hasPermi="['system:condition:edit']"
            >居住信息</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:condition:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:condition:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listCondition,
  getCondition,
  delCondition,
  addCondition,
  updateCondition,
} from "@/api/cluster/condition";
import {
  listMessage,
  getMessage,
  addMessage,
  updateMessage,
  delMessage,
  getArea,
} from "@/api/cluster/message";
export default {
  name: "Condition",
  dicts: [
    "user_documents_type",
    "user_marriage_type",
    "user_culture_extent",
    "user_occupation",
    "user_retire",
    "user_nature",
    "user_hobby",
    "user_ok",
    "user_bailout_type",
    "user_economy",
    "user_nation",
  ],
  data() {
    return {
      cityProps: {
        multiple: false,
        value: "id",
        label: "name",
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 老人健康情况表格数据
      conditionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      server: process.env.VUE_APP_SERVER_API_URL,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        oldMenId: null,
        stature: null,
        weight: null,
        waistline: null,
        historyBloodPressure: null,
        historyBloodGlucose: null,
        historyGeneticHistory: null,
        historyAnamnesis: null,
        historyDrugAllergyHistory: null,
        historyFamilyMedicalHistory: null,
        historyChronicDisease: null,
        Assessment: null,
        Ability: null,
        PhysicalCondition: null,
        PsychologicalStates: null,
        DementiaPrecursor: null,
        ExistingSymptom: null,
        Dietary: null,
        Drink: null,
        Smoking: null,
        TakeExercise: null,
        Sleep: null,
        Pharmacy: null,
      },
      // 表单参数
      form: {},
      area:[],
      // 表单校验
      rules: {
        oldMenId: [{ required: true, message: "老人id不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
    this.getArea();
  },
  methods: {
    getArea() {
      getArea().then((res) => {
        this.area = this.getTreeData(res);
      });
    },
    getTreeData(data) {
      for (let i = 0; i < data.length; i++) {
        if (data[i].children.length < 1) {
          // children若为空数组，则将children设为undefined
          delete data[i].children;
        } else {
          // children若不为空数组，则继续 递归调用 本方法
          this.getTreeData(data[i].children);
        }
      }
      return data;
    },
    /** 老人家属信息*/
    handleAddDetail(item) {
      this.$router.push(`/user/hospitalization?id=${item.id}`);
    },
    handleDetail(item) {
      this.$router.push(`/user/other?id=${item.oldManId}`);
    },
    handleStatusDetail(item) {
      this.$router.push(`/user/status?id=${item.oldManId}`);
    },
    /** 查询老人健康情况列表 */
    getList() {
      this.loading = true;
      listMessage(this.queryParams).then((response) => {
        this.conditionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        oldMenId: null,
        stature: null,
        weight: null,
        waistline: null,
        historyBloodPressure: null,
        historyBloodGlucose: null,
        historyGeneticHistory: null,
        historyAnamnesis: null,
        historyDrugAllergyHistory: null,
        historyFamilyMedicalHistory: null,
        historyChronicDisease: null,
        Assessment: null,
        Ability: null,
        PhysicalCondition: null,
        PsychologicalStates: null,
        DementiaPrecursor: null,
        ExistingSymptom: null,
        Dietary: null,
        Drink: null,
        Smoking: null,
        TakeExercise: null,
        Sleep: null,
        Pharmacy: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.city) {
          this.queryParams.residenceProvince = this.queryParams.city[0];
          this.queryParams.residenceCity = this.queryParams.city[1];
          this.queryParams.residenceArea = this.queryParams.city[2];
        }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;

      // this.title = "添加老人健康情况";
      // this.$router.push({
      //   path: "/cluster/dwell",
      // });
      this.$router.push("/user/dwell");
      // <el-table-column label="字典类型" align="center" :show-overflow-tooltip="true">
      //   <template slot-scope="scope">
      //     <router-link :to="'/system/dict-data/index/' + scope.row.dictId" class="link-type">
      //       <span>{{ scope.row.dictType }}</span>
      //     </router-link>
      //   </template>
      // </el-table-column>
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      // getMessage(id).then((response) => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改老人健康情况";
      // });

      this.$router.push({
        path: "/user/dwell",
        query: {
          id: id,
        },
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCondition(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCondition(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除老人健康情况编号为"' + ids + '"的数据项？')
        .then(function () {
          return delMessage(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/condition/export",
        {
          ...this.queryParams,
        },
        `condition_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
